﻿using System.IO;
using System.Threading;
using NExplorer.Core.Properties;

namespace NExplorer.Core.Commands
{
	/// <summary>
	/// Creates a new directory in the _currently selected_ directory.
	/// </summary>
	internal class NewFolderContextMenuCommand : CommandBase
	{
		public NewFolderContextMenuCommand()
			: base("New Folder", Resources.NewFolder.ToBitmap())
		{}

		public override void Execute()
		{
			// SampleMenuBuilder has already determined that a single directory is selected, so we can
			// make the assumptions below.
			CreateDirectory((DirectoryInfo)ServiceManager.Explorer.SelectedItems[0]);
		}

		internal static void CreateDirectory(DirectoryInfo dirInfo)
		{
			string dirName = "New Folder";
			int counter = 1;

			while (dirInfo.GetDirectories(dirName).Length > 0)
			{
				dirName = "New Folder (" + counter++ + ")";
			}

			DirectoryInfo newDir = dirInfo.CreateSubdirectory(dirName);

			ServiceManager.Explorer.SelectedItems = newDir.ToArray();
		}
	}
}